﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MipsPipelineSimulator
{
    class RunTimeCode
    {
        static private List<Code> codeList = new List<Code>();

        static private Int64 codeStartAddress = 0;

        static public void Add(Code code)
        {
            codeList.Add(code);
        }

        static public void Add(Code[] codes)
        {
            codeList.AddRange(codes);
        }

        static public Int64 CodeStartAddress
        {
            set
            {
                codeStartAddress = value;
            }
            get
            {
                return codeStartAddress;
            }
        }

        static public Code GetCode(Int64 codeCurrentAddress)
        {
            int index = Convert.ToInt32((codeCurrentAddress - CodeStartAddress)/4);
            if (index < 0 || index >= codeList.Count)
            {
                Code code = new Code();
                code.codeType = CodeType.OVER;
                return code;
            }
            return codeList[index];
        }

        static public void Clear()
        {
            codeList.Clear();
        }

        static public bool Add(string arg)
        {
            Code code = RunTimeCode.Resolve(arg);
            if (code.codeType == CodeType.ERR)
            {
                return false;
            }
            RunTimeCode.Add(code);
            return true;
        }

        static public bool Add(string [] args)
        {
            if (args == null || args.Length == 0) return false;
            foreach (string str in args)
            {
                if (!RunTimeCode.Add(str))
                {
                    return false;
                }
            }
            return true;
        }

        static private Code Resolve(string arg)
        {
            Code code = new Code();
            return code;
        }
    }
}
